﻿using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Text;
using Dotnet.Utils.Utility.ObjectExtensions.Str;

namespace Dotnet.Utils.Utility.Http
{
    public static class HttpUtil
    {
        public static string DownloadString(string url, string postData = null, IDictionary<string, string> headers = null, Encoding encoding = null)
        {
            byte[] postData2 = null;
            if (encoding == null)
            {
                encoding = Encoding.UTF8;
            }
            if (!string.IsNullOrEmpty(postData))
            {
                postData2 = postData.ToBytes(encoding);
                if (headers == null)
                {
                    headers = new Dictionary<string, string>();
                }
                if (!headers.ContainsKey("Content-Type"))
                {
                    headers.Add("Content-Type", "application/json");
                }
            }
            return DownloadString(url, postData2, headers, encoding);
        }

        public static string DownloadString(string url, byte[] postData = null, IDictionary<string, string> headers = null, Encoding encoding = null)
        {
            HttpWebRequest httpWebRequest = null;
            string result;
            try
            {
                ServicePointManager.DefaultConnectionLimit = 1000;
                httpWebRequest = (HttpWebRequest)WebRequest.Create(url);
                if (postData == null)
                {
                    httpWebRequest.Method = "GET";
                }
                else
                {
                    httpWebRequest.Method = "POST";
                    Stream requestStream = httpWebRequest.GetRequestStream();
                    requestStream.Write(postData, 0, postData.Length);
                    requestStream.Close();
                }
                if (headers != null)
                {
                    foreach (KeyValuePair<string, string> keyValuePair in headers)
                    {
                        if (keyValuePair.Key.Equals("Content-Type"))
                        {
                            httpWebRequest.ContentType = keyValuePair.Value;
                        }
                        else if (keyValuePair.Key.Equals("Referer"))
                        {
                            httpWebRequest.Referer = keyValuePair.Value;
                        }
                        else
                        {
                            httpWebRequest.Headers.Add(keyValuePair.Key, keyValuePair.Value);
                        }
                    }
                }
                using (HttpWebResponse httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse())
                {
                    string text;
                    using (StreamReader streamReader = new StreamReader(httpWebResponse.GetResponseStream(), encoding == null ? Encoding.UTF8 : encoding))
                    {
                        text = streamReader.ReadToEnd();
                    }
                    result = text;
                }
            }
            finally
            {
                if (httpWebRequest != null)
                {
                    httpWebRequest.Abort();
                }
            }
            return result;
        }
    }
}